﻿using System;
using System.Diagnostics;
using System.Web.Compilation;

namespace Globsite.Globalization.Mvc.DbResources
{
    public class DbResourceProviderFactory : ResourceProviderFactory
    {
        public override IResourceProvider CreateGlobalResourceProvider(string classKey)
        {
            Debug.WriteLine(string.Format("DbResourceProviderFactory.CreateGlobalResourceProvider({0})", classKey));

            return new DbResourceProvider(classKey, GlobsiteConfigSection.Instance.DbResources.IgnoreCase);
        }

        public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
        {
            Debug.WriteLine(string.Format("DbResourceProviderFactory.CreateLocalResourceProvider({0})", virtualPath));

            var resourceClass = getResourceClassFromVirtualPath(virtualPath);

            return new DbResourceProvider(resourceClass, GlobsiteConfigSection.Instance.DbResources.IgnoreCase);
        }

        private static string getResourceClassFromVirtualPath(string virtualPath)
        {
            if (string.IsNullOrEmpty(virtualPath))
                throw new ArgumentNullException("virtualPath");

            var resourceClass = virtualPath.Remove(0, virtualPath.IndexOf('/', 1) + 1);

            return resourceClass;
        }
    }
}
